from django.shortcuts import render
from rest_framework.response import Response
from rest_framework.views import APIView
from Processengine.models import *
from Processengine.serializers import *
# Create your views here.


# 流程 增删改查
class ShowProcessView(APIView):
    def get(self,request):

        data = ProcessModel.objects.all()
        ser = Processser(data,many=True)


        return Response({
            'code':200,
            'msg':'成功',
            'data':ser.data
        })
# 添加


class CreateProcessView(APIView):

    def post(self,request):
        proname = request.data.get('proname')
        prodate = request.data.get('prodate')

        try:
            ProcessModel.objects.create(
                proname=proname,
                prodate=prodate,
            )
        except Exception as e:
            print('添加流程表错误的原因>>>>>>>>>>>>>>',e)
            return Response({
                'code':400,
                'msg':'添加失败',
            })
        return Response({
            'code':200,
            'msg':'添加成功'
        })

# 修改


class UpdateProcessView(APIView):
    def put(self,request,id):
        proname = request.data.get('proname')
        prodate = request.data.get('prodate')
        try:
            ProcessModel.objects.filter(id=id).update(
                proname=proname,
                prodate=prodate,
            )
        except Exception as e:
            print('>>>>>>>>>>失败的原因',e)
            return Response({
                'code':400,
                'msg':'修改失败',
            })
        return Response({
            'code':200,
            'msg':'修改成功',
        })


class DeleteProcessView(APIView):
    def delete(self,request,id):
        try:
            ProcessModel.objects.filter(id=id).delete()
        except Exception as e:
            print('>>>>>>>>>>>>>>>>>删除失败的原因',e)
            return Response({
                'code':400,
                'msg':'删除失败'
            })
        return Response({
            'code':200,
            'msg':'删除成功'
        })


class ShowNodeView(APIView):
    def get(self,request):
        data = NodeModel.objects.all()
        list1=[]

        for i in data:
            aa = {
                'nodename':i.nodename,
                'pro_id':i.pro_id.proname,
            }
            list1.append(aa)
        return Response({
            'code':200,
            'msg':'获取成功',
            'data':list1,
        })

# 添加节点
class CreateNodeView(APIView):
    def post(self,request):
        nodename = request.data.get('nodename')
        try:
            NodeModel.objects.create(
                nodename=nodename,
            )
        except Exception as e:
            print('>>>>>>>>>>>>>>节点添加失败原因',e)
            return Response({
                'code':400,
                'msg':'添加失败',
            })
        return Response({
            'code':200,
            'msg':'添加成功'
        })

# 删除节点


class DeleteNodeView(APIView):
    def delete(self,request,id):
        try:
            NodeModel.objects.filter(id=id).delete()
        except Exception as e:
            print('>>>>>>>>>>删除错误',e)
            return Response({
                'code':400,
                'msg':'删除失败'
            })
        return Response({
            'code':200,
            'msg':'删除成功'
        })

# 节点属性实例
class ShowWorkflowInstanceView(APIView):
    def get(self,request):
        data = WorkflowInstanceModel.objects.all()

        list1 = []

        for i in data:
            aa = {
                'yuan_id':i.yuan_id.yname,
                'pro_id':i.pro_id.proname,
            }
            list1.append(aa)
        return Response({
            'code':200,
            'msg':'获取成功',
            'data':list1
        })

# 属性

# 节点的属性
class ShowNodeAtrView(APIView):
    def get(self,request):
        data = NodeAttributeinstanceModel.objects.all()

        list1 = []

        for i in data:
            aa = {
                'node_id':i.node_id.nodename,
                'attr_id':i.attr_id.attrname,
                'content':i.content,
            }
            list1.append(aa)
        return Response({
            'code':200,
            'msg':'获取成功',
            'data':list1,
        })

class ShowYuangongView(APIView):
    def get(self,request):
        data = YuanGongModel.objects.all()

        list = []

        for i in data:
            aa = {
                'yname':i.yname
            }
            list.append(aa)

        return Response({
            'code': 200,
            'msg': '获取成功',
            'data': list,
        })
class ShowAttributeView(APIView):
    def get(self,rrequest):
        data = AttributeModel.objects.all()

        list = []

        for i in data:
            aa = {
                'attrname':i.attrname,
                'node_id':i.node_id.nodename,
            }
            list.append(aa)
        return Response({
            'code':200,
            'msg':'成功',
            'data':list
        })

class ShowNodeAttrInstView(APIView):
    def get(self,request):
        data = NodeAttributeinstanceModel.objects.all()

        list = []

        for i in data:
            aa = {
                'node_id':i.node_id.nodename,
                'attr_id':i.attr_id.attrname,
                'content':i.content
            }
            list.append(aa)

        return Response({
            'code':200,
            'msg':'成功',
            'data':list
        })


class ShowApproverView(APIView):
    def get(self,request):
        data = ApproverTableModel.objects.all()

        list1 = []

        for i in data:
            aa = {
                'id':i.id,
                'yuan_id':i.yuan_id.yname,
                'wfi_id':i.wfi_id.pro_id.proname,
                'date':i.date,
                'operato':i.operato,
                'nodestate':i.nodestate
            }
            list1.append(aa)
        return Response({
            'code':200,
            'msg':'成功',
            'data':list1
        })






